/*
 * CS_PK_REQ_CREATE_ROOM.cpp
 *
 *  Created on: 2014. 1. 6.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../engine.h"
#include "../../User.h"
#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendAckFail(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_PK_REQ_CREATE_ROOM);
	pUser->Send(pAck);
}

/*
// 배틀로얄 방생성요청
CS_PK_REQ_CREATE_ROOM	0x1420
{
}

SC_PK_REQ_CREATE_ROOM	0x1421
{
	s16  s16ack;

	u32 u32room_number;		//방번호
	u8  u8MaxMemeberCount;	//방인원 최대 수(배틀로얄은 4 혹은 3이 될 수 있음:우선은 3)
}
*/
void Command::CS_PK_REQ_CREATE_ROOM(User * pUser, ::Network::Packet * pPacket)
{
	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAckFail(pUser, ACK_NO_SPACE);
		return;
	}

	if (false == pUser->EndBattle_afterEffect())
	{
		DEBUGLOG("%s false == pUser->EndBattle_afterEffect()", __FUNCTION__);
		SendAckFail(pUser, ACK_BATTLE_AFTEREFFECT);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAckFail(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAckFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	auto & pkRoomTree = NLogic::Static::Instance().GETPKROOMTREE();

	if (MAX_NEW_PK_ROOM <= static_cast<int>(pkRoomTree.size()))
	{
		DEBUGLOG("%s (MAX_NEW_PK_ROOM) <= pkRoomVector.size()", __FUNCTION__);
		SendAckFail(pUser, ACK_PVP_PK_OVER_ROOM_COUNT);
		return;
	}

	NLogic::IndexerPK * pPKIndex = NLogic::Static::InstancePtr()->GETPKINDEXPOOL()->NEW();
	if (NULL == pPKIndex)
	{
		DEBUGLOG("%s : pPKIndex is NULL!!", __FUNCTION__);
		SendAckFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	PREPARE_PACKET;

	// 월드에서 나감
	pUser->SetEnumState(User::EnumState::BATTLE_ROYAL_PARTY_WAIT);
	pWorld->LeaveUser(pUser);

	// 파티 생성
	NLogic::Party * pParty = NLogic::Static::Instance().GETPARTYPOOL()->NEW();
	pParty->Open(pUser);
	pParty->AddAcceptUserTree(pUser);
	pParty->SetState(NLogic::Party::State::NEW_PK_ROOM_WAIT);

	// PK Space 설정
	NLogic::Space::Data clsData;
	clsData._maxUser = MAX_PK_INVITE;
	clsData._spaceType = NLogic::Space::SpaceType::BATTLEROYAL;

	// PK Space 생성
	NLogic::Space * pBattleRoyal = NLogic::Static::Instance().GETSPACEPOOL()->NEW();
	pBattleRoyal->Open(pUser, clsData);
	// PK Space 에 파티 설정
	pBattleRoyal->SetParty(pParty);
	// Indexer 설정
	pBattleRoyal->SetPKIndexer(pPKIndex);

	// PK Space 에 입장
	pBattleRoyal->EnterUser(pUser);

	pkRoomTree[pPKIndex->GetIndex()] = pBattleRoyal;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	* pAck	<< static_cast<int16_t>(ACK_OK)
			<< static_cast<uint32_t>(pPKIndex->GetIndex())
			<< static_cast<uint8_t>(MAX_PK_INVITE);

	pAck->MakePacket(CMD_SC_PK_REQ_CREATE_ROOM);
	pUser->Send(pAck);
}

} /*NCommand*/ }  /*ProjectZ*/
